<?php
/**
 * Created by PhpStorm.
 * 资金相关类
 * @User: 王海东
 * @Date: 2018/9/7 15:52
 */

namespace app\api\controller;

use app\common\controller\BaseController;

use app\common\model\Bankinfo;
use app\common\model\DepositWithdraw;
use app\common\model\User;
use app\common\model\Capital;
use app\common\model\CapitalLog;
use think\Db;

class CapitalController extends BaseController
{
    /**
     * 在线提现
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function withdraw()
    {
        $in = $this->I([
            [
                "token|令牌",
                null,
                "s",
                "require",
            ],
            [
                "amount|提现金额",
                null,
                "f",
                "require|min:1",
            ],
            [
                "bank_id|银行卡编号",
                null,
                "d",
                "require",
            ]
        ]);

        if(is_null(UID)) {
            rj(2,'登录失效，请重新登录');
        }

        $u = new User();

        $info = $u->info(UID);

        if($info['balance'] < $in['amount']) {
            rj(1,'余额不足');
        }

        $b      = new Bankinfo();
        $bank   = $b->find($in['bank_id']);

        if(!$bank) {
            rj(1,'银行卡不存在');
        }


        $d  = new DepositWithdraw();
        $c  = new Capital();
        $cl = new CapitalLog();

        $amount = -$in['amount'];

        $insert = [
            'trade_no'  => makeOrderNo(),
            'msg'       => '在线提现',
            'fee'       => 0,
            'amount'    => $in['amount'],
            'type'      => 2,
            'bank_id'   => $in['bank_id']
        ];

        Db::startTrans();
        try {

            $d_res  = $d->add(UID,$insert);
            $c_res  = $c->setBalance(UID,$amount);
            $cl_res = $cl->add(UID,$amount,'在线提现扣除【'.$amount.'】元',2);

            if(!$d_res || !$c_res || !$cl_res) {
                Db::rollback();
                rj(1,'提现失败');
            }

            Db::commit();
            rj();
        } catch (\Exception $e) {
            Db::rollback();
            rj(1,'提现失败');
        }
    }

    /**
     * 查询用户资金流水
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function selectCapitalLog()
    {
        $in = $this->I([
            [
                "token|令牌",
                null,
                "s",
                "require",
            ],
            [
                "type|资金类型",
                '',
                "d",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        if(is_null(UID)) {
            rj(2,'登录失效，请重新登录');
        }

        $c      = new CapitalLog();
        $data   = $c->userList(UID,$in['type'],$in['page'],$in['pageSize']);

        rjd($data);
    }

    /**
     * 查询充值或提现记录
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function selectDeposit()
    {
        $in = $this->I([
            [
                "token|令牌",
                null,
                "s",
                "require",
            ],
            [
                "type|记录类型",
                null,
                "d",
                "require",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        if(is_null(UID)) {
            rj(2,'登录失效，请重新登录');
        }

        $d      = new DepositWithdraw();
        $data   = $d->userList(UID,$in['type'],$in['page'],$in['pageSize']);

        rjd($data);
    }
}